package com.fgba.express.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fgba.express.core.dao.ICrudDao;
import com.fgba.express.core.dto.BaseDto;
import com.fgba.express.core.entity.BaseEntity;
import com.fgba.express.core.mapstruct.BaseStruct;
import com.fgba.express.core.search.BaseSearch;
import com.fgba.express.core.service.ICrudService;
import com.fgba.express.core.utils.GenericUtil;
import com.fgba.express.util.NullChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class CrudServiceImpl<M extends ICrudDao<T, D, Q>, T extends BaseEntity, D extends BaseDto, Q extends BaseSearch, C extends BaseStruct<T, D, Q>> extends ServiceImpl<M, T> implements ICrudService<T, D, Q> {


    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected C struct;

    @Override
    public IPage<D> getListPage(Q search, Integer pageNum, Integer pageSize) {
        IPage<D> page = baseMapper.selectByPage(new Page<D>(pageNum, pageSize), search);
        return page;
    }

    private IPage<D> conversionToDtoIPage(IPage<T> page) {
        List<D> modelList = struct.dtoConvertEntity(page.getRecords());
        IPage<D> dtoPage = new Page<>();
        dtoPage.setRecords(modelList);
        dtoPage.setCurrent(page.getCurrent());
        dtoPage.setPages(page.getPages());
        dtoPage.setSize(page.getSize());
        dtoPage.setTotal(page.getTotal());
        return dtoPage;
    }

    @Override
    public List<D> getList(Q search) {
        List<T> entityList = baseMapper.selectList(Wrappers.emptyWrapper());
        List<D> modelList = struct.dtoConvertEntity(entityList);
        return modelList;
    }

}
